package com.ruoyi.system.service.impl;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

import com.ruoyi.system.constant.Constants;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.AllTaskMapper;
import com.ruoyi.system.mapper.TaskTypeMapper;
import com.ruoyi.system.utils.OutStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MriqcConfigMapper;
import com.ruoyi.system.service.IMriqcConfigService;


import static com.ruoyi.system.utils.Util.getsub;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-13
 */
@Service
public class MriqcConfigServiceImpl implements IMriqcConfigService
{
    @Autowired
    private MriqcConfigMapper mriqcConfigMapper;
    @Autowired
    private AllTaskMapper allTaskMapper;
    @Autowired
    private TaskTypeMapper taskTypeMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MriqcConfig selectMriqcConfigById(Long id)
    {
        return mriqcConfigMapper.selectMriqcConfigById(id);
    }

    @Override
    public MriqcConfig selectNew(MriqcConfig mriqcConfig) {
        return mriqcConfigMapper.selectMriqcConfigListNew(mriqcConfig);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param mriqcConfig 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MriqcConfig> selectMriqcConfigList(MriqcConfig mriqcConfig)
    {
        return mriqcConfigMapper.selectMriqcConfigList(mriqcConfig);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param mriqcConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMriqcConfig(MriqcConfig mriqcConfig)
    {
        return mriqcConfigMapper.insertMriqcConfig(mriqcConfig);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param mriqcConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMriqcConfig(MriqcConfig mriqcConfig)
    {
        return mriqcConfigMapper.updateMriqcConfig(mriqcConfig);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMriqcConfigByIds(Long[] ids)
    {
        return mriqcConfigMapper.deleteMriqcConfigByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMriqcConfigById(Long id)
    {
        return mriqcConfigMapper.deleteMriqcConfigById(id);
    }

    @Override
    public void runstatus2to3(){
        AllTask allTask = new AllTask();
        allTask.setStatus(2l);
        allTask.setTypeId(1l);  //typeid为1 mriqc任务
        List<AllTask> allTasks = allTaskMapper.selectAllTaskList(allTask);
        for(AllTask at_mriqc:allTasks){
            at_mriqc.setStatus(3l);
            MriqcConfig mriqcConfigtemp = new MriqcConfig();
            mriqcConfigtemp.setAllTaskId(at_mriqc.getId());
            MriqcConfig mriqcConfig = mriqcConfigMapper.selectMriqcConfigListNew(mriqcConfigtemp);
            int count = getsub(mriqcConfig.getSubject()).size();
            int min=0;
            TaskType taskType = taskTypeMapper.selectTaskTypeById(at_mriqc.getTypeId());
            min = (int) (count*taskType.getPertime());
            LocalDateTime currentDateTime = LocalDateTime.now();
            LocalDateTime newDateTime = currentDateTime.plusMinutes(min);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = newDateTime.format(formatter);
            at_mriqc.setOverTime(formattedDateTime);
            allTaskMapper.updateAllTask(at_mriqc);
        }

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        forkJoinPool.submit(() ->
                allTasks.parallelStream().forEach(at_mriqc -> {
                    MriqcConfig mriqcConfigtemp = new MriqcConfig();
                    mriqcConfigtemp.setAllTaskId(at_mriqc.getId());
                    MriqcConfig mriqcConfig = mriqcConfigMapper.selectMriqcConfigListNew(mriqcConfigtemp);
                    AllTask allTask1 = allTaskMapper.selectAllTaskById(at_mriqc.getId());
                    String task = allTask1.getId().toString();
                    String subjectsList = mriqcConfig.getSubject();

                    Process proc;
                    try {
                        String workingdic = Constants.mriqcpth;


                        String py = "python /root/web/mriqc/"+"runMRIqc.py --WorkingDir=\"" + workingdic + "\" --subjects=\""+subjectsList+
                                "\" --taskid="+task;

                        System.out.println("---------mriqc--------------"+py);

                        proc = Runtime.getRuntime().exec(py);// 执行py文件

                        OutStream error= new OutStream(proc.getErrorStream(),"Error");
                        OutStream output= new OutStream(proc.getInputStream(),"OutPut");
                        error.start();
                        output.start();
                        proc.waitFor(); //等待子进程结束

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                })
        ).join();
        forkJoinPool.shutdown();
    }


}
